﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Objects;
using Wusong.Dal.Inf;


namespace Wusong.Dal.EF
{
    public class EF4DAO<T> : Wusong.Dal.Inf.IObjDAO<T> where T : class,
        new()//约束T类型必须可以实例化
    {
        private ObjectContext objectContext;
        private DbTransaction tran;

        public EF4DAO(ObjectContext context)
        {
            objectContext = context;
        }

        public T LoadSingleEntity(Func<T, bool> whereLambda)
        {
            var objs = objectContext.CreateObjectSet<T>().Where<T>(whereLambda).AsQueryable<T>();
            if (objs.Any())
                return objs.FirstOrDefault();

            return null;
        }

        public IQueryable<T> LoadEntites(Func<T, bool> whereLambda)
        {
            var query=objectContext.CreateObjectSet<T>();
            var result= query.Where<T>(whereLambda).AsQueryable();;
            string queryStr = query.ToTraceString();
            return result;
        }

        public IQueryable<T> LoadEntites(Func<T, bool> whereLambda, int pageIndex, int pageSize, out int totalCount)
        {
            pageIndex = pageIndex - 1;
            if (pageIndex < 0)
            {
                pageIndex = 0;
            }
            var result = objectContext.CreateObjectSet<T>().Where<T>(whereLambda).AsQueryable<T>();
            totalCount = result.Count();
            return result.Skip(pageIndex * pageSize).Take(pageSize);
        }

        public T AddEntity(T entity,bool autoFlush=true)
        {
            objectContext.CreateObjectSet<T>().AddObject(entity);
            if (autoFlush)
            {
                objectContext.SaveChanges();
            }
            return entity;
        }

        public T UpdateEntity(T entity)
        {
            objectContext.CreateObjectSet<T>().Attach(entity);
            //将附加的对象状态更改为修改
            objectContext.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
            objectContext.SaveChanges();
            return entity;
        }

        public bool DelEntity(T entity)
        {
            objectContext.CreateObjectSet<T>().Attach(entity);
            //将附加的实体状态更改为删除
            objectContext.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Deleted);
            if (objectContext.SaveChanges() > 0)
            {
                return true;//删除成功
            }
            else
            {
                return false;//删除失败
            }
        }

        public bool DelEntityByWhere(Func<T, bool> whereLambda)
        {
            var tmp = objectContext.CreateObjectSet<T>().Where<T>(whereLambda);//根据条件从数据库中获取对象集合
            foreach (var entity in tmp)
            {
                objectContext.CreateObjectSet<T>().DeleteObject(entity);//标记对象为删除状态删除
            }
            if (objectContext.SaveChanges() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void BeginTran()
        {
            tran = this.objectContext.Connection.BeginTransaction();
        }

        public void EndTran()
        {
            if (tran != null)
            {
                tran.Commit();
            }
        }
    }

}
